export function oneMonthBefore() {
    let now = new Date()
    let year = now.getFullYear()
    let month = now.getMonth() + 1
    let day = now.getDate()
    let hours = now.getHours()
    let minutes = now.getMinutes()
    let seconds = now.getSeconds()
    let dateObj = {}
    dateObj.end = year + '-' + month + '-' + day
    let endMonthDay = new Date(year, month, 0).getDate() //当前月的总天数
    if (month - 1 <= 0) {
        //如果是1月，年数往前推一年<br>
        dateObj.start = year - 1 + '-' + 12 + '-' + day
    } else {
        var startMonthDay = new Date(year, parseInt(month) - 1, 0).getDate()
        if (startMonthDay < day) {
            //1个月前所在月的总天数小于现在的天日期
            if (day < endMonthDay) {
                //当前天日期小于当前月总天数
                dateObj.start =
                    year +
                    '-' +
                    (month - 1) +
                    '-' +
                    (startMonthDay - (endMonthDay - day))
            } else {
                dateObj.start = year + '-' + (month - 1) + '-' + startMonthDay
            }
        } else {
            dateObj.start = year + '-' + (month - 1) + '-' + day
        }
    }
    console.log('一月前日期', JSON.stringify(dateObj))
    dateObj.start += ` ${hours}:${minutes}:${seconds}`
    return dateObj.start.toString()
}
// 两月之前
export function twoMonthBefore() {
    let end = new Date()
    let year = end.getFullYear()
    let month = end.getMonth() + 1
    let day = end.getDate()
    let hours = end.getHours()
    let minutes = end.getMinutes()
    let seconds = end.getSeconds()
    let dateObj = {}
    dateObj.end = year + '-' + month + '-' + day
    let endMonthDay = new Date(year, month, 0).getDate() //当前月的总天数
    if (month - 2 <= 0) {
        //如果是1、2月，年数往前推一年
        let start2MonthDay = new Date(
                year - 1,
                12 - (2 - parseInt(month)),
                0
            ).getDate() //3个月前所在月的总天数
        if (start2MonthDay < day) {
            //3个月前所在月的总天数小于现在的天日期
            dateObj.start =
                year - 1 + '-' + (12 - (2 - month)) + '-' + start2MonthDay
        } else {
            dateObj.start = year - 1 + '-' + (12 - (2 - month)) + '-' + day
        }
    } else {
        let start2MonthDay = new Date(year, parseInt(month) - 2, 0).getDate() //3个月前所在月的总天数
        if (start2MonthDay < day) {
            //3个月前所在月的总天数小于现在的天日期
            if (day < endMonthDay) {
                //当前天日期小于当前月总天数,2月份比较特殊的月份
                dateObj.start =
                    year +
                    '-' +
                    (month - 3) +
                    '-' +
                    (start2MonthDay - (endMonthDay - day))
            } else {
                dateObj.start = year + '-' + (month - 2) + '-' + start2MonthDay
            }
        } else {
            dateObj.start = year + '-' + (month - 2) + '-' + day
        }
    }
    console.log('二月前日期', JSON.stringify(dateObj))

    dateObj.start += ` ${hours}:${minutes}:${seconds}`
    return dateObj.start.toString()
}

// 三月之前
export function threeMonthBefore() {
    let end = new Date()
    let year = end.getFullYear()
    let month = end.getMonth() + 1
    let day = end.getDate()
    let hours = end.getHours()
    let minutes = end.getMinutes()
    let seconds = end.getSeconds()
    let dateObj = {}
    dateObj.end = year + '-' + month + '-' + day
    let endMonthDay = new Date(year, month, 0).getDate() //当前月的总天数
    if (month - 3 <= 0) {
        //如果是1、2、3月，年数往前推一年
        let start3MonthDay = new Date(
                year - 1,
                12 - (3 - parseInt(month)),
                0
            ).getDate() //3个月前所在月的总天数
        if (start3MonthDay < day) {
            //3个月前所在月的总天数小于现在的天日期
            dateObj.start =
                year - 1 + '-' + (12 - (3 - month)) + '-' + start3MonthDay
        } else {
            dateObj.start = year - 1 + '-' + (12 - (3 - month)) + '-' + day
        }
    } else {
        let start3MonthDay = new Date(year, parseInt(month) - 3, 0).getDate() //3个月前所在月的总天数
        if (start3MonthDay < day) {
            //3个月前所在月的总天数小于现在的天日期
            if (day < endMonthDay) {
                //当前天日期小于当前月总天数,2月份比较特殊的月份
                dateObj.start =
                    year +
                    '-' +
                    (month - 3) +
                    '-' +
                    (start3MonthDay - (endMonthDay - day))
            } else {
                dateObj.start = year + '-' + (month - 3) + '-' + start3MonthDay
            }
        } else {
            dateObj.start = year + '-' + (month - 3) + '-' + day
        }
    }
    console.log('三月之前', JSON.stringify(dateObj))
    dateObj.start += ` ${hours}:${minutes}:${seconds}`
    return dateObj.start.toString()

}